<!-- src/views/HomeView.vue -->
<script setup lang="ts">
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user.ts'
import UserInfoBar from '@/components/UserInfoBar.vue'
import {
  User,
  Goods,
  Document,
  Lock,
  Avatar,
  Promotion
} from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

onMounted(() => {
  // 检查登录状态
  const isLoggedIn = userStore.isLoggedIn

  if (!isLoggedIn) {
    // 未登录，跳转到登录页
    router.push('/login')
  }
  // 如果已登录，留在当前页面（主页）
})

// 处理菜单项点击事件
const handleMenuSelect = (index: string) => {
  switch (index) {
    case '1':
      router.push('/users')
      break
    case '2':
      router.push('/products')
      break
    case '3':
      router.push('/orders')
      break
    case '4':
      router.push('/permissions')
      break
    case '5':
      router.push('/roles')
      break
    case '6':
      router.push('/advertisements')
      break
  }
}
</script>

<template>
  <el-container style="height: 100vh;">
    <el-header style="height: 60px; padding: 0; box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);">
      <div style="display: flex; justify-content: space-between; align-items: center; height: 100%; padding: 0 20px;">
        <h2>管理系统</h2>
        <UserInfoBar />
      </div>
    </el-header>

    <el-container>
      <el-aside width="200px" style="background-color: #f5f5f5; height: calc(100vh - 60px);">
        <el-menu
          default-active="1"
          class="el-menu-vertical-demo"
          style="border: none;"
          @select="handleMenuSelect"
        >
          <el-menu-item index="1">
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item index="2">
            <el-icon><Goods /></el-icon>
            <span>商品管理</span>
          </el-menu-item>
          <el-menu-item index="3">
            <el-icon><Document /></el-icon>
            <span>订单管理</span>
          </el-menu-item>
          <el-menu-item index="4">
            <el-icon><Lock /></el-icon>
            <span>权限管理</span>
          </el-menu-item>
          <el-menu-item index="5">
            <el-icon><Avatar /></el-icon>
            <span>角色管理</span>
          </el-menu-item>
          <el-menu-item index="6">
            <el-icon><Promotion /></el-icon>
            <span>广告管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<style scoped>
.el-menu-item.is-active {
  background-color: #e6f7ff;
  color: #409eff;
}
</style>
